using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace ArkanoidX.Objects
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Galaxy : BaseComponent
    {
        Texture2D _texture;

        Rectangle boundingRect;

        public Vector2 Speed = new Vector2(0.01f, 0.01f);   // default speed, pixels per milisecond
        protected Vector2 currPos = Vector2.Zero;

        public Galaxy(Game game)
            : base(game)
        {
            // TODO: Construct any child components here

            _texture = game.Content.Load<Texture2D>("Galaxy");

            boundingRect = ((MainGame)game).Arena;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            currPos += Speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            while (currPos.X > boundingRect.Width)
                currPos.X -= boundingRect.Width;
            while (currPos.X < 0)
                currPos.X += boundingRect.Width;

            while (currPos.Y > boundingRect.Height)
                currPos.Y -= boundingRect.Height;
            while (currPos.Y < 0)
                currPos.Y += boundingRect.Height;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            Rectangle rect1;
            Rectangle rect2;
            Rectangle rect3;
            Rectangle rect4;

            int x = (int)currPos.X;
            int y = (int)currPos.Y;
            rect1 = new Rectangle(x, y, boundingRect.Width, boundingRect.Height);
            rect2 = new Rectangle(x - boundingRect.Width, y, boundingRect.Width, boundingRect.Height);
            rect3 = new Rectangle(x, y - boundingRect.Height, boundingRect.Width, boundingRect.Height);
            rect4 = new Rectangle(x - boundingRect.Width, y - boundingRect.Height, boundingRect.Width, boundingRect.Height);

            sBatch.Draw(_texture, rect1, _texture.Bounds, Color.White);
            sBatch.Draw(_texture, rect2, _texture.Bounds, Color.White);
            sBatch.Draw(_texture, rect3, _texture.Bounds, Color.White);
            sBatch.Draw(_texture, rect4, _texture.Bounds, Color.White);

            base.Draw(gameTime);
        }
    }
}
